//
//  ZpHomeTableViewController.m
//  WeiBo_OC
//
//  Created by MacBook Pro on 16/5/23.
//  Copyright © 2016年 MacBook Pro. All rights reserved.
//

#import "ZpHomeTableViewController.h"
#import "ZpDetailViewController.h"
#import "ZpBarButtonItem.h"
#import "ZpVisitorLoginView.h"
#import "ZpStatusCell.h"
#import "ZpStatusListViewModel.h"
#import "ZpRefreshControl.h"

@interface ZpHomeTableViewController ()
@property (nonatomic, strong) ZpStatusListViewModel *viewModels;
@end

@implementation ZpHomeTableViewController



- (void)viewDidLoad {
    [super viewDidLoad];
//    self.navigationController.navigationBar insertSubview:<#(nonnull UIView *)#> atIndex:<#(NSInteger)#>
    self.viewModels = [[ZpStatusListViewModel alloc] init];
    
    [self.tableView registerClass:[ZpStatusCell class] forCellReuseIdentifier:statusCell];
    [self setUI];
    
    self.tableView.estimatedRowHeight = 300;
   

}

//- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
//    UIResponder *response = self.nextResponder;
//    while (response) {
//        NSLog(@"%@", response);
//        response = response.nextResponder;
//    }
//}


- (void)setUI{
    self.tableView.showsHorizontalScrollIndicator = NO;
    ZpBarButtonItem *item = [ZpBarButtonItem barButtonItemWithImageName:@"navigationbar_pop" andTitle:nil andTarget:self andSelecor:@selector(pushDetailViewController) andTitleNormalColor:nil andTitleHightColor:nil];
    
    self.navigationItem.rightBarButtonItem = item;
    
    self.refreshControl = [[ZpRefreshControl alloc] init];
    self.refreshControl.attributedTitle = [[NSAttributedString alloc] initWithString:@"下载中"];
    [self.refreshControl addTarget:self action:@selector(loadMoreStatusData) forControlEvents:UIControlEventValueChanged];
    
    if (self.userAccountViewModel.userAccount == nil) {
        [self setVisitorLoginViewWithImageName:@"visitordiscover_feed_image_house" andContent:@"关注一些人,回到这里看看有什么惊喜" andIsHome:YES];
        return ;
    }
    
    [self.viewModels sendReuqestForStatus:^(){
        [self.tableView reloadData];
    }];

}

- (void)loadMoreStatusData{
    NSLog(@"123123");
    [self.viewModels sendReuqestForStatus:^{
        [self.tableView reloadData];
    }];
    [self.refreshControl endRefreshing];
    
    
}

- (void)setVisitorLoginViewWithImageName:(NSString *)imageName andContent:(NSString *)content andIsHome:(BOOL)isHome{
    
    ZpVisitorLoginView *v = (ZpVisitorLoginView *)self.view;
    
    [v setUISizeWithImageName:imageName andContentLabel:content];
    [v hiddenCircleImage:isHome];
    [v startAnimation];

}



- (void)pushDetailViewController{
    
    
    ZpDetailViewController *vc = [[ZpDetailViewController alloc] init];
    ZpBarButtonItem *item = (ZpBarButtonItem *)vc.navigationItem.leftBarButtonItem;
    UIButton *btn = [(UIButton *)item valueForKey:@"btn"];
    [btn setTitle:@"首页" forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
    
    [btn sizeToFit];

    self.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:vc animated:YES];

    
}



#pragma mark - 数据源

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return self.viewModels.statusViewModel.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    ZpStatusCell *cell = [tableView dequeueReusableCellWithIdentifier:statusCell forIndexPath:indexPath];
    cell.statusViewModel = self.viewModels.statusViewModel[indexPath.row];

    return cell;
}



@end
